package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/1/24 14:57
 * @apiNote
 */

public class Test00557ReverseWords {
    public static void main(String[] args) {
        System.out.println(reverseWords("Let's take LeetCode contest"));
    }

    public static String reverseWords(String s) {
        String[] strs = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0 ; i < strs.length; i++) {
            strs[i] = reverse(strs[i].toCharArray());
        }
        for (int i = 0 ; i < strs.length; i++) {
            sb.append(strs[i]).append(" ");
        }
        return sb.substring(0,sb.length()-1);
    }

    public static String reverse(char[] chars) {
        int start = 0;
        int end = chars.length - 1;
        while (start < end) {
            char temp = chars[start];
            chars[start] = chars[end];
            chars[end] = temp;
            start++;
            end--;
        }
        return new String(chars);
    }
}
